home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / sendmail / uk-sendmail2.1 / Support / warn.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-06-11  |  3.9 KB  |  180 lines

  1. #include <stdio.h>
  2.  
  3. /*
  4.  *  routines to maintain a list of mailfiles for which warning messages have
  5.  *  been sent out, plus routines to send out warning and failure messages.
  6.  *
  7.  *  Written by Jim Crammond    <jim@cs.hw.ac.uk>    3/86
  8.  */
  9.  
  10. #define SENDMAIL   "/usr/lib/sendmail"
  11. #define NAMESIZE   15
  12. #define BLKSIZE    ((BUFSIZ/NAMESIZE) - 1)
  13.  
  14. struct flist
  15. {    char    fname[BLKSIZE][NAMESIZE];
  16.     int    nused;
  17.     struct    flist *next;
  18. };
  19.  
  20.  
  21. struct    flist    warnlist;
  22. FILE    *warnfp;
  23.  
  24.  
  25. /*
  26.  *  Initialise list of files for which warning messages have already been sent.
  27.  *  This involves reading the warnfile into a table, removing files which
  28.  *  no longer exist (i.e. been sent or deleted), and writing this out again.
  29.  */
  30. init_warnedlist(warnfile)
  31. char    *warnfile;
  32. {
  33.     struct    flist    *wp;
  34.     char    warned[NAMESIZE], *p;
  35.     int    i;
  36.     char    *index();
  37.  
  38.     wp = &warnlist;
  39.     wp->next = NULL;
  40.     wp->nused = 0;
  41.  
  42.     if ((warnfp = fopen(warnfile, "r")) != NULL)
  43.     {    while (fgets(warned, NAMESIZE, warnfp) != NULL)
  44.         {    if ((p = index(warned, '\n')) != NULL)
  45.                 *p = '\0';
  46.  
  47.             if (exists(warned))
  48.             {    if (wp->nused >= BLKSIZE)
  49.                 {    wp->next = (struct flist *) malloc(sizeof(warnlist));
  50.                     wp = wp->next;
  51.                     wp->next = (struct flist *) NULL;
  52.                     wp->nused = 0;
  53.                 }
  54.                 strcpy(wp->fname[wp->nused], warned);
  55.                 wp->nused++;
  56.             }
  57.         }
  58.         fclose(warnfp);
  59.     }
  60.  
  61.     /*
  62.      *  Rewrite warnedlist removing files that no longer exist.
  63.      *  Could be really paranoid here and create a temporary file
  64.      *  first, rather than overwrite; in case of crashed
  65.      */
  66.     if ((warnfp = fopen(warnfile, "w")) != NULL)
  67.     {    wp = &warnlist;
  68.         while (wp)
  69.         {    for (i=0; i < wp->nused; i++)
  70.                 fprintf(warnfp, "%s\n", wp->fname[i]);
  71.             wp = wp->next;
  72.         }
  73.         fflush(warnfp);
  74.     }
  75. }
  76.  
  77. /*
  78.  *  Determine whether the given filename is in the warn list.
  79.  *  Returns 1 if found, 0 otherwise.
  80.  */
  81. in_warnedlist(file)
  82. char    *file;
  83. {
  84.     struct    flist    *wp = &warnlist;
  85.     int    i;
  86.  
  87.     while (wp)
  88.     {    for (i=0; i < wp->nused; i++)
  89.         {    if (strcmp(file, wp->fname[i]) == 0)
  90.                 return(1);
  91.         }
  92.         wp = wp->next;
  93.     }
  94.     return(0);
  95. }
  96.  
  97. /*
  98.  *  Add a filename to the warn list.
  99.  */
  100. add_warnedlist(file)
  101. char    *file;
  102. {
  103.     fprintf(warnfp, "%s\n", file);
  104. }
  105.  
  106.  
  107. /*
  108.  *  Send a Failed Mail message back to the sender, containing the whole
  109.  *  of the failed message.
  110.  */
  111. sendfailure(sender, rcpts, host, hours, msgfile)
  112. char    *sender;
  113. char    **rcpts;
  114. char    *host;
  115. int    hours;
  116. char    *msgfile;
  117. {
  118.     FILE    *out, *popen();
  119.     char    cmd[50];
  120.  
  121.     sprintf(cmd, "%s -t", SENDMAIL);
  122.     out = popen(cmd, "w");
  123.     fprintf(out, "From: MAILER-DAEMON\nSubject:Failed Mail\nTo: %s\n\n", sender);
  124.     fprintf(out, "After %d days (%d hours), your message to the following people:\n\n", hours/24, hours);
  125.  
  126.     /* put out recipents */
  127.     while (*rcpts)
  128.     {    fprintf(out, "\t%s  (host=%s)\n", *rcpts, host);
  129.         rcpts++;
  130.     }
  131.  
  132.     fprintf(out, "\ncould not be delivered.\n\n"); 
  133.     fprintf(out, "   ----- Unsent message follows -----   \n");
  134.  
  135.     /*  print all of the message */
  136.     print_message(msgfile, out, 0);
  137.     pclose(out);
  138.  
  139.     return;
  140. }
  141.  
  142.  
  143. /*
  144.  *  Send a Waiting Mail message back to the sender, containing a summary
  145.  *  of the delayed message (to remind him/her what it was about!).
  146.  */
  147. sendwarning(sender, rcpts, host, hours, failtime, msgfile)
  148. char    *sender;
  149. char    **rcpts;
  150. char    *host;
  151. int    hours;
  152. int    failtime;
  153. char    *msgfile;
  154. {
  155.     FILE    *out, *popen();
  156.     char    cmd[50];
  157.  
  158.     sprintf(cmd, "%s -t", SENDMAIL);
  159.     out = popen(cmd, "w");
  160.     fprintf(out, "From: MAILER-DAEMON\nSubject:Waiting Mail\nTo: %s\n\n",
  161.              sender);
  162.     fprintf(out, "After %d days (%d hours), your message to the following people:\n\n", hours/24, hours);
  163.  
  164.     /* put out recipents */
  165.     while (*rcpts)
  166.     {    fprintf(out, "\t%s  (host=%s)\n", *rcpts, host);
  167.         rcpts++;
  168.     }
  169.  
  170.     fprintf(out, "\nhas not yet been delivered. Attempts to deliver the message will\n");
  171.     fprintf(out, "continue for %d more days. No further action is required by you.\n\n", (failtime-hours)/24);
  172.     fprintf(out, "   ----- Queued message begins -----   \n");
  173.  
  174.     /*  print a summary of the message */
  175.     print_message(msgfile, out, 1);
  176.     pclose(out);
  177.  
  178.     return;
  179. }
  180.